﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using TFXK.BLL;
using TFXK.Common;
using log4net;
using TFXK.Model;

public partial class GN_Order : System.Web.UI.Page
{

    private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private static readonly CategoryBLL bllCategory = new CategoryBLL();
    private static readonly TravelLinesBLL bllTravelLines = new TravelLinesBLL();
    private static readonly OrdersBLL bllOrders = new OrdersBLL();
    public TravelLines modelTemp = new TravelLines();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                this.Title += "-国内旅游";
                if (string.IsNullOrEmpty(Request.QueryString["Key"] + ""))
                {
                    Response.Redirect("GN_Travels.aspx");
                }
                else
                {
                    int Key = int.Parse(Request.QueryString["Key"] + "");
                    InitData(Key);
                }
            } try
            {
                if (string.IsNullOrEmpty(Session["UserLogin"] + ""))
                {

                    Msg.ShowAndRedirect("未登录或登录已超时!", Request.UrlReferrer.ToString());
                }
            }
            catch
            {
                Msg.ShowAndRedirect("未登录或登录已超时!", "CJ_Travels.aspx");
            }
        }
        catch (Exception ex)
        {
            log.Error(ex.Message);
        }
    }
    public void InitData(int Key)
    {
        modelTemp = bllTravelLines.GetModel(Key);
        txtDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
        if (string.IsNullOrEmpty(modelTemp.imagePath))
        {
            modelTemp.imagePath = "images/nopic.jpg";
        }
        else
        {
            modelTemp.imagePath = "uploads/" + modelTemp.imagePath;
        }
    }
    #region 提交
    protected void btnSubmit_Click(object sender, ImageClickEventArgs e)
    {
        string uname = StringUtil.HtmlEncode(txtOrderName.Value.Trim());
        string uPhone = StringUtil.HtmlEncode(this.txtPhone.Value.Trim());
        string uTel = StringUtil.HtmlEncode(this.txtTel.Value.Trim());
        string oNum = StringUtil.HtmlEncode(this.txtOrderNum.Value.Trim());
        //string date = 
        string date = StringUtil.HtmlEncode(txtDate.Value.Trim());
        string desc = StringUtil.HtmlEncode(this.txtDes.Value.Trim());

        if (string.IsNullOrEmpty(uname) || string.IsNullOrEmpty(uPhone) || string.IsNullOrEmpty(date))
        {
            Msg.Show("信息不能为空！");
            return;
        }
        else
        {
            Orders model = new Orders();
            model.createDate = DateTime.Now;
            try
            {
                model.customID = ((Customs)Session["UserLogin"]).id;
            }
            catch {
                model.customID = 1;
            }
            try
            {
                model.orderDate = DateTime.Parse(date);
            }
            catch
            {
                Msg.Show("日期输入不正确！");
                return;
            }
            model.orderDes = desc;
            model.orderNum = int.Parse(oNum);
            model.orderState = 0;
            model.orderType = 1;
            model.parentID = int.Parse(Request.QueryString["Key"] + "");
            model.userName = uname;
            model.userSex = radioBoy.Checked ? 0 : 1;
            model.userTel = uPhone + "  " + uTel;
            int row = bllOrders.Add(model);
            if (row > 0)
            {
                Msg.ShowAndRedirect("下单成功！", "GN_TravelInfo.aspx?Key=" + model.parentID);
            }
            else
            {
                Msg.Show("下单失败！");
            }
        }
    }
    #endregion

    public string GetPrice(object obj)
    {
        string money = "0.00";
        try
        {
            return int.Parse(obj + "").ToString("c");
        }
        catch
        {

        }
        return money;
    }
}
